//
//  ReservationViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/4/16.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "ReservationViewC.h"
#import "Util.h"
#import "MyCell.h"
#import "HeadView.h"
#import "PlaceEntity.h"
#import "BusinessModel.h"
#import "UserModel.h"
#import "MyOrderViewC.h"

@interface TimeTableCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *timeLab;
@end
@implementation TimeTableCell

@end

@interface ReservationCCell : UICollectionViewCell
@property (nonatomic, weak) IBOutlet UILabel *dayLab;
@property (nonatomic, weak) IBOutlet UILabel *weekLab;
@end
@implementation ReservationCCell

@end

@interface ReservationViewC (){
    UITableView *mytableview;
    NSMutableArray *newArray;
    NSMutableArray *dateArray;
    NSArray *timeArray;
    NSArray *dataArray;
    NSArray *priceArray;
}

@end

@implementation ReservationViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title =@"场馆预定";
    newArray = [[NSMutableArray alloc] init];
    dateArray = [[NSMutableArray alloc] init];
    [self getSevenDays];
    [self getData];
    
    _scorllView.frame =CGRectMake(50, 60, MAIN_SCREEN_SIZE.width - 60, _scorllView.frame.size.height);
    
    _scorllView.bounces=NO;
    for (int i = 0; i<10; i++) {
        mytableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 60 * i, _scorllView.frame.size.height )];
        mytableview.delegate = self;
        mytableview.dataSource = self;
        mytableview.bounces = NO;
        mytableview.backgroundColor = [UIColor clearColor];
        mytableview.separatorStyle=UITableViewCellSeparatorStyleNone;
        mytableview.showsHorizontalScrollIndicator = NO;
        mytableview.showsVerticalScrollIndicator = NO;
    }
    [_scorllView addSubview:mytableview];
    _scorllView.contentSize = CGSizeMake(60 * 9, 0);
    
    UIBarButtonItem *rightBtn1 = [[UIBarButtonItem alloc]
                                  initWithImage:[UIImage imageNamed:@"mail"]
                                  style:UIBarButtonItemStylePlain
                                  target:self action:@selector(mailAction:)];
    self.navigationItem.rightBarButtonItem = rightBtn1;

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self getData];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)mailAction:(id)sender{
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                   bundle:nil];
    MyOrderViewC *myomvc = [main instantiateViewControllerWithIdentifier:@"MyOrderViewC"];
    [self.navigationController pushViewController:myomvc animated:YES];
}

- (void)getSevenDays{
    
    NSDate*nowDate = [NSDate date];
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MM/dd"];
    NSDateFormatter *dateFormatter1=[[NSDateFormatter alloc]init];
    [dateFormatter1 setDateFormat:@"yyyy-mm-dd"];
    NSDate* theDate;
    for (int i = 0; i<7; i++) {
        NSTimeInterval  oneDay = 24*60*60*1;  //1天的长度
        theDate = [nowDate initWithTimeIntervalSinceNow:oneDay*i];
        NSString *allDate=[dateFormatter stringFromDate :theDate];
        NSString *allDate1=[dateFormatter1 stringFromDate :theDate];
        NSString *date = [Util weekdayStringFromDate:theDate];
        if ([date isEqualToString:@"Sunday"]) {
            date = @"周日";
        }
        NSDictionary *dic = @{@"date":allDate,@"week":date,@"allday":allDate1};
        [newArray addObject:dic];
    }
    
    for (NSDictionary *dict in newArray) {
        PlaceEntity *plaEty = [[PlaceEntity alloc] initWithAttributes:dict];
        [dateArray addObject:plaEty];
    }
    
}

- (void)getData{
    
    if ([UserModel sharedInstance].currentUser == nil) {
        [Util presentLoginControllerFromViewController:self];
    }
    
    else{
    NSString *sporth = self.sportHid;
    NSString *sporte = self.sportEid;
    NSString *stime = self.time;
    NSString *sacid = [NSString stringWithFormat:@"%@",
                       [UserModel sharedInstance].currentUser.ID];
    [[BusinessModel sharedInstance] getReservationWithSporthall:sporth scheduledTime:stime accountId:sacid Sportevent:sporte returnData:^(id info) {
        NSDictionary *dic = [info copy];
        dataArray = [NSArray arrayWithObject:dic];
        timeArray = info[@"timesolt"];
        priceArray = info[@"statusList"];
        [_timeTableview reloadData];
        [mytableview reloadData];
    }];
        
    }
    
}



#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _timeTableview) {
        return timeArray.count;
    }
    return priceArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 40.f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == mytableview) {
        return 36.f;
    }
    
    else
        return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (tableView == _timeTableview) {
    
        TimeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TimeTabID"
                                               forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString *time = timeArray[indexPath.row];
        cell.timeLab.text = [NSString stringWithFormat:@"%@点",time];
          return cell;

    }
    
    if (tableView == mytableview) {
        MyCell *cell=[[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.delegate=self;
        cell.backgroundColor=[UIColor clearColor];
//        cell.getdetail = @"15";
        cell.textLabel.text = @"15元";
        cell.detailTextLabel.text = @"00";
        cell.textLabel.textColor = [UIColor whiteColor];
        return cell;
    }
    
    else{

        static NSString *cellIdentifier=@"cell";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY= mytableview.contentOffset.y;
    CGPoint timeOffsetY= _timeTableview.contentOffset;
    timeOffsetY.y=offsetY;
    _timeTableview.contentOffset=timeOffsetY;
    if(offsetY==0){
        _timeTableview.contentOffset=CGPointZero;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return dateArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
        ReservationCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReservationCCellID" forIndexPath:indexPath];
        PlaceEntity *placeEty = dateArray[indexPath.row];
        cell.dayLab.text = [NSString stringWithFormat:@"%@",placeEty.day];
        cell.weekLab.text = [NSString stringWithFormat:@"%@",placeEty.week];
    
        return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PlaceEntity *placeEty = dateArray[indexPath.row];
    self.time = [NSString stringWithFormat:@"%@",placeEty.allday];
    [self getData];

}

@end
